"""
线程队列
"""
from threading import Thread
from queue import Queue
import time
# class Spider(object):
#     def __init__(self):
#         self.q = Queue()
#     def parse_put_job(self):
#         for day in range(1,51):
#             data = f"今天是第{day}天, 我依然爱你"
#             self.q.put(data)
#         print("进程t1执行结束")
#     def parse_get_job(self):
#         for day in range(1,51):
#             data = self.q.get()
#             print(data)
#         print('进程t2执行结束')
#     def run(self):
#         t1 = Thread(target=self.parse_put_job)
#         t2 = Thread(target=self.parse_get_job)
#         t1.start()
#         t2.start()
# if __name__ == '__main__':
#     s = Spider()
#     s.run()
#     print('主进程执行结束')
#-------------------------------------------------
# class Spider(object):
#     def __init__(self):
#         self.q = Queue()
#     def parse_put_job(self):
#         for day in range(1,51):
#             data = f"今天是第{day}天, 我依然爱你"
#             self.q.put(data)
#         print("线程t1执行结束")
#     def parse_get_job(self):
#         while True:
#             data = self.q.get()
#             print(data)
#     def run(self):
#         t1 = Thread(target=self.parse_put_job)
#         t2 = Thread(target=self.parse_get_job)
#         t1.start()
#         t2.daemon = True
#         t2.start()
# if __name__ == '__main__':
#     s = Spider()
#     s.run()
#     time.sleep(0.5)
#     print('主进程执行结束')
#-------------------------------------------------
# class Spider(object):
#     def __init__(self):
#         self.q = Queue()
#     def parse_put_job(self):
#         for day in range(1,51):
#             data = f"今天是第{day}天, 我依然爱你"
#             self.q.put(data)
#         print("线程t1执行结束")
#     def parse_get_job(self):
#         while True:
#             data = self.q.get()
#             print(data)
#     def run(self):
#         t1 = Thread(target=self.parse_put_job)
#         t2 = Thread(target=self.parse_get_job)
#         t1.start()
#         t2.daemon = True
#         t2.start()
#         t1.join()#会发现打印结果跟预想的不一样因为线程的执行速度太快了(线程的执行速度比进程快)
# if __name__ == '__main__':
#     s = Spider()
#     s.run()
#     print('主进程执行结束')
#-----------------------------------------------
#正确1
# class Spider(object):
#     def __init__(self):
#         self.q = Queue()
#     def parse_put_job(self):
#         for day in range(1,51):
#             data = f"今天是第{day}天, 我依然爱你"
#             self.q.put(data)
#             self.q.join()
#         print("线程t1执行结束")
#     def parse_get_job(self):
#         while True:
#             data = self.q.get()
#             print(data)
#             self.q.task_done()
#     def run(self):
#         t1 = Thread(target=self.parse_put_job)
#         t2 = Thread(target=self.parse_get_job)
#         t1.start()
#         t2.daemon = True
#         t2.start()
#         t1.join()
# if __name__ == '__main__':
#     s = Spider()
#     s.run()
#     print('主进程执行结束')
#---------------------------------------
#正确2
class Spider(object):
    def __init__(self):
        self.q = Queue()
    def parse_put_job(self):
        for day in range(1,51):
            data = f"今天是第{day}天, 我依然爱你"
            self.q.put(data)
        self.q.join()
        print("线程t1执行结束")
    def parse_get_job(self):
        while True:
            data = self.q.get()
            print(data)
            self.q.task_done()
    def run(self):
        t1 = Thread(target=self.parse_put_job)
        t2 = Thread(target=self.parse_get_job)
        t1.start()
        t2.daemon = True
        t2.start()
        t1.join()
if __name__ == '__main__':
    s = Spider()
    s.run()
    print('主进程执行结束')